package com.cooper.mediadb.online;

import android.os.Bundle;
import android.view.View;

import com.cooper.android.comm.DataChangedListener;
import com.cooper.mediadb.Media;
import com.cooper.mediadb.MediaBrowseBaseFragment;
import com.cooper.mediadb.MediaInfoDialog;
import com.cooper.scrapemedialib.R;
import com.cooper.slidingmenu.utils.SingleFragmentContain;

public class OnlineMediaInfoDialog extends MediaInfoDialog<OnlineMedia>
		implements DataChangedListener {

	private OnlineMedia media;

	@Override
	public void onActivityCreated(Bundle savedState) {
		super.onActivityCreated(savedState);

		
		if (getMedia().detailsIsReady()) {
			director.setText(getMedia().getDirector());
			plot.setText(getMedia().getPlot());
			setActorsText();
		} else {
			media.setDetailsReadyListener(this);
			getMedia().prepareDetails();
			waitDetailsView();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected OnlineMedia getMedia() {
		if (media == null) {
			media = ((MediaBrowseBaseFragment<OnlineMedia>) ((SingleFragmentContain) getActivity())
					.getCurFragment()).getCurrentMedia();
		}
		return media;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if (media != null)
			media.cancelDetails();
	}

	@Override
	public void onNotifyDataChanged(Object sender, int what, Object data) {
		if (what == Media.DETAILS_IS_OK) {
			director.setText(getMedia().getDirector());
			plot.setText(getMedia().getPlot());
			setMovieDetailsViews();

			setActorsText();
		} else {
			director.setText(R.string.down_movie_details_fail);
			plot.setText(R.string.down_movie_details_fail);
			actors.setText(R.string.down_movie_details_fail);
		}
		poltProgress.setVisibility(View.GONE);
		directorProgress.setVisibility(View.GONE);
		actorProgress.setVisibility(View.GONE);
	}

	private void waitDetailsView() {
		director.setText(R.string.wait_get_details);
		plot.setText(R.string.wait_get_details);
		actors.setText(R.string.wait_get_details);
		poltProgress.setVisibility(View.VISIBLE);
		directorProgress.setVisibility(View.VISIBLE);
		actorProgress.setVisibility(View.VISIBLE);
	}

}
